#!/usr/bin/env python3
"""
插入unit_view表假数据的Python脚本
"""

import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from sqlalchemy.orm import Session
from datetime import datetime
from app.database import SessionLocal, init_db
from app.models.unit_view import UnitView


def insert_unit_view_data():
    """插入unit_view表的假数据"""

    # 确保数据库表已创建
    init_db()

    # 创建数据库会话
    db = SessionLocal()

    try:
        # 检查是否已存在数据，避免重复插入
        existing_count = db.query(UnitView).count()
        if existing_count > 0:
            print(f"数据库中已存在 {existing_count} 条unit_view数据，跳过插入")
            return

        # 准备假数据
        fake_data = [
            {
                "sn_code": "SN001234567890",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN002345678901",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN003456789012",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN004567890123",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN005678901234",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN006789012345",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN007890123456",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN008901234567",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN009012345678",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
            {
                "sn_code": "SN010123456789",
                "top_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "bottom_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "left_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
                "right_view": "uploads/70711665-8d6e-4d4c-a910-02037b9e8bd1.jpg",
            },
        ]

        # 批量插入数据
        unit_views = []
        for data in fake_data:
            unit_view = UnitView(**data)
            unit_views.append(unit_view)

        db.add_all(unit_views)
        db.commit()

        print(f"成功插入 {len(unit_views)} 条unit_view数据")

        # 查询并显示插入的数据
        inserted_data = db.query(UnitView).order_by(UnitView.created_at.desc()).all()
        print("\n插入的数据：")
        for item in inserted_data:
            print(f"ID: {item.id}, SN码: {item.sn_code}, 创建时间: {item.created_at}")

    except Exception as e:
        db.rollback()
        print(f"插入数据时发生错误: {e}")

    finally:
        db.close()


if __name__ == "__main__":
    insert_unit_view_data()
